clear all

*Define global path for replication package
global path "~/Dropbox/IT_Revolution/Replication_package/JPE submission"

global path_rawdata "$path/Raw_data"
global path_cleandata "$path/Clean_data"
global path_output "$path/Output"

cap mkdir "$path_output"

***Step 1: Estimate regressions for ICT exposure, 1980-2019
********************************************************************************
use "$path_cleandata/data_occ_1980_2010_s1_a29distance.dta", clear

foreach var of varlist emp1980 emp1990 emp2000 emp2010 emp2018 {
	egen tot_emp = sum(`var'), by(group)
	replace `var' = `var'/tot_emp
	drop tot_emp
}

*create variables for regressions)
gen change_emp4 = log(emp2018/emp1980)
gen change_inc4  = lavgwage2018  - lavgwage1980 

*select occupations with positive employment in 1980 and 2018
gen aind = change_emp4 != .
egen ind = sum(aind), by(occ1990dd)
keep if ind == 3

*weights
gen aemp1980 = emp1980 if group == 0
egen emp_all0 = mean(aemp1980), by(occ1990dd)

*standardized exposure measures
foreach var of varlist ict {
	sum `var' if group == 0 [aw=emp_all0]
	gen exposure_`var' = (`var' - `r(mean)')/`r(sd)' 
}
egen aad_ent = std(ad_ent)

*produce the estimates
local exposure exposure_ict 
local dist aad_ent
local controls 

label variable exposure_ict exposure

gen exposure = `exposure'
gen exposure_dist = c.`exposure'#c.`dist'

reg  `dist' exposure if group == 0 [aw = emp_all0]  , cluster(occ)
est sto reg_exp_dist_post
	
foreach p of numlist 4 {
	reg change_emp`p' exposure exposure_dist `controls' if group == 0 [aw = emp_all0]  , cluster(occ1990dd)
	est sto per`p'_emp_dist_post
	
	reg change_inc`p' exposure exposure_dist  `controls' if group == 0 [aw = emp_all0]  , cluster(occ1990dd)
	est sto per`p'_inc_dist_post
}

***Step 2: Estimate regressions for manufacturin exposure, 1900-1940
********************************************************************************
use "$path_cleandata/data_occ_1900_1940_s1_a29distance.dta", clear

*create variables for regressions
gen change_emp4 = log(emp1940/emp1900)

*select occupations with positive employment in 1900 and 1940
gen aind = change_emp4 != .
egen ind = sum(aind), by(occ)
keep if ind == 3

*weights
gen aemp1900 = emp1900 if group == 0
egen emp_all0 = mean(aemp1900), by(occ)

*standardized exposure measures
foreach var of varlist manuf1900 {
	sum `var' if group == 0 [aw=emp_all0]
	gen exposure_`var' = (`var' - `r(mean)')/`r(sd)' 
}
rename (exposure_manuf1900 ) (exposure_manuf )

egen aad_ent = std(ad_ent)

*produce the estimates
local exposure exposure_manuf 
local dist aad_ent
local controls 

label variable exposure_manuf exposure

gen exposure = `exposure'
gen exposure_dist = c.`exposure'#c.`dist'

reg  `dist' exposure if group == 0 [aw = emp_all0] , cluster(occ)
est sto reg_exp_dist_pre
	
foreach p of numlist 4 {
	reg change_emp`p' exposure exposure_dist `controls' if group == 0 [aw = emp_all0]  , cluster(occ)
	est sto per`p'_emp_dist_pre
}

*Step 3: Export the estimates
********************************************************************************

esttab reg_exp_dist_post per4_emp_dist_post per4_inc_dist_post reg_exp_dist_pre per4_emp_dist_pre using "$path_output/Table1_distance.tex",	///
		cells(b(nostar fmt(%9.3f)) se(par)) starlevels(* 0.10 ** 0.05 *** 0.01) stats(,) fragment booktabs extracols(1 4) style(tex)	///
		varlabels(exposure "Exposure\$_o\$" exposure_dist "Exposure\$_o\times\$TaskDistance\$_o\$")	///
		collabels(none) mlabels(none) nonumber nocons nor2 noobs nonotes substitute(\_ \ \midrule "") replace
